package com.qingxin.sys.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysRole;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 角色表
 * @author Administrator
 *
 */
@Service
public class SysRoleService extends BaseService<SysRole, String> {

	
	/**
	 * 角色列表数据
	 * @param search
	 * @param loginUser
	 * @param pageParam
	 * @param request
	 * @return
	 */
	public Pager list(String groupid, String search, LoginUser loginUser, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.sys.entity.SysRole(r.roleid, r.role_name, r.create_time, g.groupname)" +
				" from SysRole r, SysGroup g where r.groupid=g.groupid ");
		
		if(StringUtils.isNotEmpty(groupid) && !"0".equals(groupid)){
			querier.append(" and r.groupid like :groupid");
			querier.setParam("groupid", groupid.replace("'", "") + "%");
		}
		
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (r.role_name like :role_name or r.role_code like :role_code)");
			querier.setParam("role_name", search);
			querier.setParam("role_code", search);
		}
		
		querier.append(pageParam.getOrderby(" order by r.create_time desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
		
	}
	
	
	/**
	 * 添加角色
	 * @param role
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(SysRole role, LoginUser loginUser, HttpServletRequest request){
		if(isNameDuplicate(role.getRole_name(), null, role.getGroupid())){
			return new SysError("角色名称已存在.");
		}
		
		Date now = new Date();
		role.setCreate_time(now);
		role.setUpdate_time(now);
		role.setCreate_by(loginUser.getLogin_name());
		role.setUpdate_by(loginUser.getLogin_name());
		
		role.setRole_code("");
		
		this.save(role);
		return null;
	}
	
	
	/**
	 * 修改角色信息
	 * @param role
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(SysRole role, LoginUser loginUser, HttpServletRequest request){
		SysRole old = getSafeEntity(role.getRoleid());
		if(old == null){
			return new SysError("角色信息不存在");
		}
		
		if(isNameDuplicate(role.getRole_name(), role.getRoleid(), role.getGroupid())){
			return new SysError("角色名称已存在.");
		}
		
		String fields = "role_name,remark,subgroup";
		BeanUtil.setTargetFields(old, role, fields, true);
		
		old.setUpdate_by(loginUser.getLogin_name());
		old.setUpdate_time(new Date());
		
		Updater<SysRole> updater = new Updater<>(old, fields+",update_by,update_time");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除角色信息
	 * @param roleids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	@CacheEvict(value="userRightsCache", allEntries = true)
	public SysError delete(String[] roleids, LoginUser loginUser, HttpServletRequest request){
		for(String roleid : roleids){
			SysRole role = get(roleid);
			if(role == null){
				continue;
			}
			
			if(role.getRole_code()!=null && role.getRole_code().startsWith("sys_role_")){
				return new SysError("角色【"+role.getRole_name()+"】为系统保留角色，不可删除.");
			}
			
			this.delete(role);
		}
		return null;
	}
	
	
	
	/**
	 * 判断指定角色名称是否被使用
	 * @param role_name
	 * @param roleid
	 * @return
	 */
	public boolean isNameDuplicate(String role_name, String roleid, String groupid){
		if(StringUtils.isEmpty(roleid)){
			Object val = getByHql("select roleid from SysRole where role_name=? and groupid=?", role_name, groupid);
			return val!=null;
		}else{
			Object val = getByHql("select roleid from SysRole where role_name=? and groupid=? and roleid<>?", role_name, groupid, roleid);
			return val!=null;
		}
	}
	
	
	
}
